/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import multimedia.control.event.SetNextMediaElementEvent;
	import multimedia.control.event.UpdateRssFeedPlayStatusEvent;
	import multimedia.model.ModelLocator;
	import multimedia.model.vo.ImageVO;
	import multimedia.model.vo.MediaElementVO;
	import multimedia.model.vo.TextVO;
	import multimedia.model.vo.VideoVO;
	import multimedia.util.LogUtil;
	
	import mx.logging.ILogger;
	
	import org.osmf.events.LoadEvent;
	import org.osmf.events.MediaElementEvent;
	import org.osmf.events.TimeEvent;
	import org.osmf.layout.LayoutMetadata;
	import org.osmf.traits.LoadState;
	import org.osmf.traits.LoadTrait;
	import org.osmf.traits.MediaTraitType;

	public class SetNextMediaElementCommand extends SequenceCommand implements ICommand
	{
		private var model:ModelLocator;
		private var nextElement:MediaElementVO;
		
		private static const LOG:ILogger = LogUtil.getLogger(SetNextMediaElementCommand);
		
		override public function execute(event:CairngormEvent):void
		{
			LOG.debug("<<< Setting next media Element >>>");
			this.nextEvent = (event as SetNextMediaElementEvent).nextEvent;
			model = ModelLocator.getInstance();
			model.sourceEnabled = false;
			if(model.errors == 5)
			{
				LOG.error("<<< GOT 5 ERRORS => SHOW ERROR SCREEN >>>");
				model.errors = 0;
				model.mediaElements[(model.counter + 1)%4] = model.errorElement;
			}
			if(model.mediaElements[(model.counter + 1)%4] != null)
			{
				nextElement = model.mediaElements[(model.counter + 1)%4] as MediaElementVO;
				if(model.currentMediaElement != null)
				{
					model.wrapper.removeMediaElement(model.currentMediaElement);
				}
				if(nextElement.mediaElement.hasTrait(MediaTraitType.LOAD))
				{
					var load:LoadTrait = nextElement.mediaElement.getTrait(MediaTraitType.LOAD) as LoadTrait;
					if(load.loadState == LoadState.READY) {
						LOG.debug("<<< {0} Load Trait ready >>>", nextElement.media.mediaId);
						setMedia();
					} else {
						LOG.debug("<<< {0} adding event listener for load trait >>>", nextElement.media.mediaId);
						load.addEventListener(LoadEvent.LOAD_STATE_CHANGE, function(event:LoadEvent):void {
							if(event.loadState == LoadState.READY) {
								LOG.debug("<<< {0} Load Trait ready >>>", nextElement.media.mediaId);
								setMedia()
							}
							if(event.loadState == LoadState.LOAD_ERROR)
							{
								LOG.error("<<< error while loading media: {0}>>>", event.type);
							}
						});
						if(model.wrapper.mediaPlayer.playing) {
							LOG.debug("<<< PAUSING player for loading >>>");
							model.wrapper.mediaPlayer.seek(0);
							model.wrapper.mediaPlayer.pause();
							
						}
					}
				}
				else
				{
					setMedia()
				}
			}
		}
		
		private function setMedia():void
		{
			LOG.debug("<<< {0} setting media >>>", nextElement.media.mediaId);
			
			model.wrapper.addMediaElement(nextElement.mediaElement);
			model.wrapper.media = nextElement.mediaElement;
			model.currentMedia = nextElement.media;
			model.currentMediaElement = nextElement.mediaElement;
			model.captionEnabled = false;
			if(model.currentMedia is TextVO)
			{
				if((model.currentMedia as TextVO).background_image != null || (model.currentMedia as TextVO).background_image != "") {
					model.background_color = 0xffffff;
				} else {
					model.background_color = (model.currentMedia as TextVO).backgroundColor;
				}
			} else
			{
				model.background_color = 0;
			}
			if(model.currentMedia is VideoVO)
			{
				model.captionEnabled = (model.currentMedia as VideoVO).subtitles != "";
			}
			if(model.currentMedia is ImageVO)
			{
				model.sourceEnabled = true;
				var metadata:LayoutMetadata = model.currentMediaElement.getMetadata(LayoutMetadata.LAYOUT_NAMESPACE) as LayoutMetadata;
				model.sourceLabel.x = metadata.x;
				model.sourceLabel.text = "Quelle: " + (model.currentMedia as ImageVO).image_source
			}
			new UpdateRssFeedPlayStatusEvent(model.currentMedia.newsticker).dispatch();
			LOG.debug("<<< {0} starting media to play  >>>", model.currentMedia.mediaId);
			model.wrapper.mediaPlayer.play();
			model.mediaElements[model.counter] = null;
			model.counter = (model.counter + 1)%4;
			
			executeNextCommand();
		}
		
	}
}